package xuegao.study.mall.admin.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.pms.PmsProduct;
import xuegao.study.mall.common.param.pms.PmsProductParam;
import xuegao.study.mall.common.param.pms.PmsProductResult;
import xuegao.study.mall.admin.service.pms.PmsProductService;

import java.util.List;


/**
 * 商品管理controller
 */
@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    private PmsProductService pmsProductService;

    @ApiOperation("商品分页查询")
    @GetMapping("/list")
    public CommonResult<CommonPage<PmsProduct>> getList(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<PmsProduct> list = pmsProductService.list(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("根据商品id获取商品编辑信息")
    @GetMapping("/updateInfo/{id}")
    public CommonResult<PmsProductResult> getUpdateInfo(@PathVariable Long id) {
        PmsProductResult productResult = pmsProductService.getUpdateInfo(id);
        return CommonResult.success(productResult);
    }

    @PostMapping("/create")
    public CommonResult create(@RequestBody PmsProductParam productParam) {
        int count = pmsProductService.create(productParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Long id, @RequestBody PmsProductParam productParam) {
        int count = pmsProductService.update(id, productParam);
        if (count > 0) {
            return CommonResult.success("ok");
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        int count = pmsProductService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
